<?php

namespace Admintmd\Controller;
use Think\Controller;

class LoginController extends BaseController{
	
	public function index(){
		$this->display();
	}
	//计算所有用户的流水并加入表
    public function user_liusui()
    {
        //列出所有用户
        $member = M('user');
        $list = $member->where(['id'=>'5000'])->select();
        foreach ($list as $k=>$v){
            $user_order = M("order")->field("sum(del_points) as sum_del")->where("userid={$v['id']} && state=1  && is_add=1 ")->find();
            if ($user_order['sum_del']){
                $data['liushui']=$user_order['sum_del'];
                M('user')->where("id={$v['id']}")->save($data);
            }
        }
    }
	public function login(){
		if(IS_POST){
			if(!IS_AJAX){
				$this->error('提交方式不正确！');
			}else{
				$username = I('username');
				$password = md5(I('password'));
				
				$remember = I('remember');
                $where=['username'=>$username,'password'=>$password,'status'=>1,'role'=>'盘管理人员'];
                $res = M('admin')->where($where)->find();
				if($res){
					if($remember){
						session(array('name'=>'admintmd','expire'=>3600*24*3));
						session('admintmd',$res);
					}else{
						session(array('name'=>'admintmd','expire'=>3600));
						session('admintmd',$res);
					}
					$map['last_ip'] = get_client_ip();
					$map['last_time'] = time();
					M('admin')->where("id = {$res['id']}")->save($map);
					$this->success('登录成功,跳转中~',U('Index/index'),1);
				}else{
					$this->error('用户名或密码错误');
				}
			}
		}
	}




	public function logout(){
		session('admintmd',null);
		$this->redirect('Login/index');
	}
	
}

?>